package com.wxy.demo.executor;

import java.util.ArrayList;
import java.util.concurrent.*;

/**
 * <p>
 * Future
 * </p>
 *
 * @author e-Xiaoyuan.Wang
 * @since 2022/10/10 18:38
 */
public class Demo3 {
    public static void main(String[] args) {
        ExecutorService service = Executors.newWorkStealingPool();
        ArrayList<Future<?>> list = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            int finalI = i;
            Future<?> future = service.submit(() -> {
                System.out.println("线程执行：" + finalI);
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException ignored) {

                }
            });
            list.add(future);
        }
        list.forEach(future -> {
            try {
                future.get(1, TimeUnit.SECONDS);
            } catch (InterruptedException | ExecutionException | TimeoutException e) {
                e.printStackTrace();
            }
        });
    }
}
